<?php
 
namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\crm_deal;
use App\crm_order;
use App\crm_users;
use App\crm_customer;
use Encore\Admin\Auth\Database\Permission;
use App\Admin\Extensions\Tools\BumenGender;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\ChengjiaoGender;

class Crm_chengjiaoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){
            $content->header('成交单统计');
            $content->description('统计');
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            $kehu=crm_customer::all();//客户
            $order=crm_order::all();
            $list=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->paginate(1);
            $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->get();
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $a[$k]=$vo->deal_yglirun;
                }
                foreach($maoli as $k=>$vo){
                    $b[$k]=$vo->deal_yshoukuan-$vo->deal_sshoukuan;
                }
                foreach($maoli as $k=>$vo){
                    $c[$k]=$vo->deal_ydakuan-$vo->deal_sdakuan;
                }
                $ygzongji=array_sum($a);
                $yshoukuan=array_sum($b);
                $dakuan=array_sum($c);
            }else{
                $ygzongji=0;
                $yshoukuan=0;
                $dakuan=0;
            }
            $res['list']=$list;
            $res['customer']=$kehu;
            $res['order']=$order;
            $res['ygzongji']=$ygzongji;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $res['yshoukuan']=$yshoukuan;
            $res['dakuan']=$dakuan;
            $content->body(new ChengjiaoGender($res));
        });
    }
    // 表单提交后
    public function chengjiao(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {
            $content->header('成交单统计');
            $content->description('统计');
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            $list=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->paginate(1);
            $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->get();
            $query = DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3);
            if($request['bumen']){
                $aa=DB::table('admin_user_permissions')->where("permission_id",$request['bumen'])->pluck("user_id");
                $list=$query->whereIn('deal_userid',$aa)->paginate(1);
                $maoli=$query->whereIn('deal_userid',$aa)->get();
            }
            if($request['yuangong']){
                $list=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->where('deal_userid',$request['yuangong'])->paginate(1);
                $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->where('deal_userid',$request['yuangong'])->get();
                $query=DB::table('crm_deal')->where('deal_jiesuanstatue','!=',3)->where('deal_userid',$request['yuangong']);
            }
            if($request['order_lvyoudate']){
                if($request['order_lvyoudate']['start']){
                    $list=$query->where('deal_dealdate', '>', substr($request['order_lvyoudate']['start'], 2))->paginate(1);
                    $maoli=$query->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2))->get();
                    $query=$query->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2));
                }
                if($request['order_lvyoudate']['end']){
                    $list=$query->where('deal_dealdate', '<', substr($request['order_lvyoudate']['end'], 2))->paginate(1);
                    $maoli=$query->where('deal_dealdate', '<',substr($request['order_lvyoudate']['end'], 2))->get();
                }
            }
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $a[$k]=$vo->deal_yglirun;
                }
                foreach($maoli as $k=>$vo){
                    $b[$k]=$vo->deal_yshoukuan-$vo->deal_sshoukuan;
                }
                foreach($maoli as $k=>$vo){
                    $c[$k]=$vo->deal_ydakuan-$vo->deal_sdakuan;
                }
                $ygzongji=array_sum($a);
                $yshoukuan=array_sum($b);
                $dakuan=array_sum($c);
            }else{
                $ygzongji=0;
                $yshoukuan=0;
                $dakuan=0;
            }
            $kehu=crm_customer::all();
            $order=crm_order::all();
            $res['list']=$list;
            $res['customer']=$kehu;
            $res['order']=$order;
            $res['ygzongji']=$ygzongji;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $res['yshoukuan']=$yshoukuan;
            $res['dakuan']=$dakuan;
            $content->body(new ChengjiaoGender($res));
        });

    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_deal::class, function (Grid $grid) {
            $grid->model()->where("deal_jiesuanstatue","!=",3);
            $grid->column("客户")->display(function () {
                return crm_order::find($this->deal_orderid)->crm_customer->customer_name;
            });
            $grid->column("电话")->display(function () {
                return crm_order::find($this->deal_orderid)->crm_customer->customer_phone;
            });
            $grid->column("线路名称")->display(function () {
                return crm_order::find($this->deal_orderid)->order_xianlu;
            });
            $grid->deal_yuangong("员工姓名");
            $grid->deal_yshoukuan("应收金额");
            $grid->deal_ydakuan("应付金额");
            $grid->deal_yglirun("预估毛利");
            $grid->deal_ftdate("发团日期");
            $grid->deal_dealdate("下单日期");
            $grid->tools(function ($tools) {
                $list=Permission::all()->pluck('name', 'id');
                $tools->append(new BumenGender($list));

                $tools->append(new YgmaoliGender());
                $tools->append(new SjmaoliGender());
            });
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $list=crm_users::all()->pluck("username","username");
                $filter->equal('deal_bumen','部门')->select(Permission::all()->pluck('name', 'id'));
                $filter->equal('deal_yuangong','员工姓名')->select($list);
                $filter->between('deal_dealdate', '下单日期')->datetime();
            });
           $script_addones=<<<EOT
                $(".f5299650cd9b7e9b68b08a756d39c25d").change(function(){
                    var id=$(".f5299650cd9b7e9b68b08a756d39c25d option:selected").val();
                    $.ajax({
                        url: "/admin/gdindex/ajax13",
                        type: "get",
                        dataType: "json",
                        data: {'v': id},
                        success:function(data){
                            var hh="<option value>请-选-择<\/option>";
                            for(var i=0;i<data.length;i++){
                                hh +="<option value="
                                hh +=data[i].id
                                hh +=" >"
                                hh +=data[i].username
                                hh +="<\/option>";
                               
                            }
                            $(".deal_yuangong").html(hh);
                        }
                    }); 
                })

EOT;
            Admin::script($script_addones);
            $grid->disableCreation();//禁用创建按钮
            $grid->disableRowSelector();//禁用行选择
            $grid->disableActions();//禁用行操作
            $grid->disableExport();//禁用导出按钮

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_deal::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    public function ajax()
    {
        $id = $_GET['v'];
        $list=\DB::table("admin_user_permissions")->where("permission_id",$id)->pluck("user_id");
        if(!$list->isEmpty()){
            $aa="";
            foreach($list as $value){
                $aa.=$value.",";
            }
            $bb=rtrim($aa, ",");
            $res1=\DB::select('select * from admin_users where id in ('.$bb.')');
            $res2=crm_users::where("id",$list)->pluck("username","id");
            
            return json_encode($res1);
        }else{
            return json_encode("");
        }
       
    }

}
